/*@@

    Copyright (c) 2010 BlueSky LTD.  All rights reserved. 

    Created_datetime : 2010-9-7 15:23:56

    File Name :	base_time.cpp

    Author : Wang Hu (ghoststears@gmail.com)

    Description : 

@@*/


#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif

#include "basetype.h"
#include "base/base_time.h"

namespace BLK
{


long UnixTime()
{
    return static_cast<long>(TimeInMilliseconds() / 1000);
}

INT64 TimeInMilliseconds()
{
#ifdef _WIN32
    
    FILETIME   ft;

    ::GetSystemTimeAsFileTime (&ft);

    /**
     * In the beginning (Jan. 1, 1601), there was no time and no computer.
     * And Bill said: "Let there be time," and there was time....
     */
    const UINT64 FILETIME_TO_TIMVAL_SKEW = (0X19DB1DED53E8000);

    ULARGE_INTEGER ns;
    ns.LowPart = ft.dwLowDateTime;
    ns.HighPart = ft.dwHighDateTime;

    ns.QuadPart -= FILETIME_TO_TIMVAL_SKEW;

    // Convert 100ns units to seconds;
    return  (INT64) (ns.QuadPart / (10000));

#else

    struct timeval tval;

    gettimeofday(&tval, NULL);
    INT64 sec = tval.tv_sec;
    INT64 usec = tval.tv_usec;
    INT64 result = sec * 1000 + usec / 1000;

    return result;

#endif
}

};
